package dateBase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import Judge.JudgeBean;
import site.SiteBean;


public class Judge_Site_DBHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "sql.db";
    private static final int DB_VERSION = 1;
    private static final String TABLE_JUDGE_NAME = "judge";
    private static final String TABLE_SITE_NAME = "site";
    private static Judge_Site_DBHelper mHelper = null;//只生成一个构造数据库的实例
    private SQLiteDatabase mRDB = null;//读链接
    private SQLiteDatabase mWDB = null;//写链接

    private Judge_Site_DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    public static Judge_Site_DBHelper getInstance(Context context) {
        if (mHelper == null) {
            mHelper = new Judge_Site_DBHelper(context);
        }
        return mHelper;
    }

    //打开数据库的读链接
    public SQLiteDatabase openReadLink() {
        if (mRDB == null || !mRDB.isOpen()) {
            mRDB = mHelper.getReadableDatabase();
        }
        return mRDB;
    }

    //打开数据库的写链接
    public SQLiteDatabase openWriteLink() {
        if (mWDB == null || !mWDB.isOpen()) {
            mWDB = mHelper.getWritableDatabase();
        }
        return mWDB;
    }

    public void closeLink() {

        if (mRDB != null && mRDB.isOpen()) {
            mRDB.close();
            mRDB = null;
        }

        if (mWDB != null && mWDB.isOpen()) {
            mWDB.close();
            mWDB = null;
        }
    }


    //新创建表CREATE TABLE [judge] (其实用不到，只有该表不存在时，才用的上
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_JUDGE_NAME + " (" +
                "jno char(9) PRIMARY KEY NOT NULL COLLATE NOCASE," +
                "jname char(25) NOT NULL COLLATE NOCASE," +
                "jsex char(2) COLLATE NOCASE DEFAULT '男'," +
                "jage char(20) COLLATE NOCASE," +
                "jproject tchar(20) COLLATE NOCASE);";


        db.execSQL(sql);
        sql = "CREATE TABLE IF NOT EXISTS " + TABLE_SITE_NAME + " (" +
                "sno char(9) PRIMARY KEY NOT NULL COLLATE NOCASE," +
                "sname char(25) NOT NULL COLLATE NOCASE," +
                "scapacity char(10) COLLATE NOCASE ," +
                "stypes char(20) COLLATE NOCASE," +
                "saddress char(20) COLLATE NOCASE);";

        db.execSQL(sql);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        return;
    }

    public long insert(JudgeBean judge) {
        ContentValues values = new ContentValues();//用键值对形式存储
        values.put("jname", judge.getJname());
        values.put("jno", judge.getJno());
        values.put("jsex", judge.getJsex());
        values.put("jproject", judge.getJproject());
        values.put("jage", judge.getJage());

        return mWDB.insert(TABLE_JUDGE_NAME, null, values);//返回插入的行号，失败返回-1
        //第二个参数是为了防止values均为空，报错的情况
    }


    public long deleteByName(String name) {

        return mWDB.delete(TABLE_JUDGE_NAME, "jname=?", new String[]{name});
    }

    //查询
    public List<JudgeBean> queryByProject(String project) {
        List<JudgeBean> list = new ArrayList<>();
        Cursor cursor = mRDB.query(TABLE_JUDGE_NAME, null, "jproject=?", new String[]{project}, null, null, null);

        while (cursor.moveToNext()) {
            JudgeBean judge = new JudgeBean();
            judge.setJno(cursor.getString(0));
            judge.setJname(cursor.getString(1));
            judge.setJsex(cursor.getString(2));
            judge.setJage(cursor.getString(3));
            judge.setJproject(cursor.getString(4));
            list.add(judge);
        }
        return list;
    }

    public List<JudgeBean> queryByName(String name) {
        List<JudgeBean> list = new ArrayList<>();
        Cursor cursor = mRDB.query(TABLE_JUDGE_NAME, null, "jname=?", new String[]{name}, null, null, null);

        while (cursor.moveToNext()) {
            JudgeBean judge = new JudgeBean();
            judge.setJno(cursor.getString(0));
            judge.setJname(cursor.getString(1));
            judge.setJsex(cursor.getString(2));
            judge.setJage(cursor.getString(3));
            judge.setJproject(cursor.getString(4));
            list.add(judge);
        }
        return list;
    }

    public long update(String type, String newData, String name) {
        ContentValues values = new ContentValues();
        String fact_type = "";
        if (type.equals("编号")) {
            fact_type = "jno";
        } else if (type.equals("姓名")) {
            fact_type = "jname";
        } else if (type.equals("年龄")) {
            fact_type = "jage";
        } else if (type.equals("性别")) {
            fact_type = "jsex";
        } else if (type.equals("管理项目")) {
            fact_type = "jproject";
        }
        if (name == "") {
            return 0;
        } else {
            values.put(fact_type, newData);
            return mWDB.update(TABLE_JUDGE_NAME, values, "jname=?", new String[]{name});
        }

    }

//---------------------------------------------------------------------

    //下面是site方法

    public long insert(SiteBean site) {
        ContentValues values = new ContentValues();//用键值对形式存储
        values.put("sno", site.getSno());
        values.put("sname", site.getSname());
        values.put("scapacity", site.getScapacity());
        values.put("stypes", site.getStypes());
        values.put("saddress", site.getSaddress());
        Log.v("MainActivity", site.getSno());
        return mWDB.insert(TABLE_SITE_NAME, null, values);//返回插入的行号，失败返回-1
        //第二个参数是为了防止values均为空，报错的情况
    }

    public long deleteByMingCheng(String name) {

        return mWDB.delete(TABLE_SITE_NAME, "sname=?", new String[]{name});
    }

    public List<SiteBean> queryByMingCheng(String name) {
        List<SiteBean> list = new ArrayList<>();
        Cursor cursor = mRDB.query(TABLE_SITE_NAME, null, "sname=?", new String[]{name}, null, null, null);

        while (cursor.moveToNext()) {
            SiteBean site = new SiteBean();
            site.setSno(cursor.getString(0));
            site.setSname(cursor.getString(1));
            site.setScapacity(cursor.getString(2));
            site.setStypes(cursor.getString(3));
            site.setSaddress(cursor.getString(4));
            list.add(site);
        }
        return list;
    }

    public long s_update(String stype, String snewData, String sname) {
        ContentValues values = new ContentValues();
        String fact_type = "";
        if (snewData == "") {
            return 0;
        }
        if (stype.equals("编号")) {
            fact_type = "sno";
        } else if (stype.equals("场地名称")) {
            fact_type = "sname";
        } else if (stype.equals("占地面积")) {
            fact_type = "scapacity";
        } else if (stype.equals("比赛类型")) {
            fact_type = "stypes";
        } else if (stype.equals("比赛场地")) {
            fact_type = "saddress";
        }

        values.put(fact_type, snewData);
        return mWDB.update(TABLE_SITE_NAME, values, "sname=?", new String[]{sname});

    }

    public List<SiteBean> queryByType(String type) {
        List<SiteBean> list = new ArrayList<>();
        Cursor cursor = mRDB.query(TABLE_SITE_NAME, null, "stypes=?", new String[]{type}, null, null, null);

        while (cursor.moveToNext()) {
            SiteBean site = new SiteBean();
            site.setSno(cursor.getString(0));
            site.setSname(cursor.getString(1));
            site.setScapacity(cursor.getString(2));
            site.setStypes(cursor.getString(3));
            site.setSaddress(cursor.getString(4));
            list.add(site);
        }
        return list;
    }

}
